package com.lang.classloader;

import java.util.ArrayList;
import java.util.List;

/**
 * 找不到类定义
 * https://blog.csdn.net/u011200604/article/details/51956654
 */
public class NoClassDefFoundErrorDueToStaticInitFailure {
    public static void main(String args[]) {
        List users = new ArrayList(2);
        for (int i = 0; i < 2; i++) {
            try {
                users.add(new User(String.valueOf(i))); //will throw NoClassDefFoundError
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }
}

class User {
    private static String USER_ID = getUserId();

    public User(String id) {
        this.USER_ID = id;
    }

    private static String getUserId() {
        throw new RuntimeException("UserId Not found");
    }
}

/**
 * output
 *
 * java.lang.ExceptionInInitializerError
 * 	at com.lang.classloader.NoClassDefFoundErrorDueToStaticInitFailure.main(NoClassDefFoundErrorDueToStaticInitFailure.java:12)
 * Caused by: java.lang.RuntimeException: UserId Not found
 * 	at com.lang.classloader.User.getUserId(NoClassDefFoundErrorDueToStaticInitFailure.java:28)
 * 	at com.lang.classloader.User.<clinit>(NoClassDefFoundErrorDueToStaticInitFailure.java:21)
 * 	... 1 more
 * java.lang.NoClassDefFoundError: Could not initialize class com.lang.classloader.User
 * 	at com.lang.classloader.NoClassDefFoundErrorDueToStaticInitFailure.main(NoClassDefFoundErrorDueToStaticInitFailure.java:12)
 */


